package UIComponents
{
	import Classes.GameUtils.GU_filters;
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.staticinfo.Language;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	public class FrameCollectItem extends Sprite
	{
		/**
		 * 购买按钮 
		 */		
		private var _button_buy:MyButton;
		/**
		 * 物品价格显示文本 
		 */		
		private var _buy_price_text:TextField;
		/**
		 * 包含购买按钮的容器 
		 */		
		private var _collectItemContainer:DisplayObjectContainer;
		/**
		 * 物品图片容器 
		 */		
		private var _exchangeItemSp:IconContainer;
		/**
		 * 物品拥有数目 
		 */		
		private var _haveItemNum:TextField;
		
		public var _mb:DisplayObject;
		
		public function FrameCollectItem()
		{
			init();
		}
		
		private function init():void
		{
			this._mb=this.getChildByName("mb");
			this._collectItemContainer=this.getChildByName("collectItemContainer") as DisplayObjectContainer;
			this._button_buy=_collectItemContainer.getChildByName("buyExchangeItem") as MyButton;
			this._buy_price_text=_collectItemContainer.getChildByName("txt_itemValue") as TextField;
			this._exchangeItemSp=this.getChildByName("iconContainer") as IconContainer;
			this._haveItemNum=((this.getChildByName("collectNum") as DisplayObjectContainer).getChildByName("txt_num")) as TextField;
//			(this.getChildByName("collectNum") as DisplayObjectContainer).visible=false;
			
			this._buy_price_text.selectable=false;
			this._buy_price_text.mouseEnabled=false;
			this.controlMoenyIcon(FlatInfo.isOPenQQVip);
			this.addListener();
		}
		
		public function controlMoenyIcon(qb:Boolean=true):void
		{
			var ms:DisplayObjectContainer=this._collectItemContainer.getChildByName("moneySp") as DisplayObjectContainer;
			ms.getChildByName("qdian").visible=qb;
			ms.getChildByName("obIcon").visible=!qb;
		}
		
		private var shopObj:IGD_ShopItem;
		public function setFrameData(obj:Object):Boolean
		{
			var tmpFlag:Boolean=true;
			this.reset();
			this._mb.visible=false;
			this.setItemNum(obj.needItemNum);
			//变灰
			if(obj.needItemNum<1)(obj.needItemIcon as DisplayObject).filters	=	[GU_filters.grayfilter];
			
			this.setItemPic(obj.needItemIcon,obj.needItemName+"："+obj.needItemDes);
			this.shopObj=obj.IDg_shop;
			this.setItemPrice(String(this.shopObj.price));
			if(Number(obj.needItemNum)<1)
			{
				tmpFlag=false;
			}
			
			
			
			this.setBuyTip();
			
			return tmpFlag;
		}
		
		private function reset():void
		{
			this._buy_price_text.text="0";
//			this._haveItemNum.text="0";
			(this.getChildByName("collectNum") as Sprite)["num"]=0;
		}
		
		/**
		 * 物品价格 
		 * @param value
		 */	
		public function setItemPrice(value:String):void
		{
			this._buy_price_text.text=value;
		}
		
		/**
		 * 物品数量 
		 * @param value
		 */		
		public function setItemNum(value:Number):void
		{
//			this._haveItemNum.text=String(value);
			(this.getChildByName("collectNum") as Sprite)["num"]=value;
		}
		/**
		 * 物品图片  加热感
		 * @param value
		 */	
		public function setItemPic(value:DisplayObject,itemName:String=""):void
		{
			value.width=60.15;
			value.height=64.85;
			this._exchangeItemSp.addPic(value,itemName);
		}
		
		private function addListener():void
		{
			_button_buy.addEventListener(ComponentEvent.BUTTON_DOWN,useHandler);
			this.getChildByName("btnWish").addEventListener(ComponentEvent.BUTTON_DOWN,clickWishHandler);
		}
		
		private function removeListener():void
		{
			_button_buy.removeEventListener(ComponentEvent.BUTTON_DOWN,useHandler);
		}
		
		private function clickWishHandler(e:Event):void
		{
			this.dispatchEvent(new Event("clickCollectWish",true));
		}
		
		private function useHandler(e:ComponentEvent):void
		{
			//点击购买按钮触发的逻辑
			this.shopObj.sendBuyCommand();
		}
		
		/**
		 *设置购买按扭热感 
		 * 
		 */
		private function setBuyTip():void
		{
			//购买按钮
			if(null!=this.shopObj)
			{
				var moenytype:String=FlatInfo.isOPenQQVip?Language.Qdian:Language.ob;
				
				var upBuyTips:String=Language.upBuyBtnTips;
				while(upBuyTips.indexOf("@needItemName")!=-1)
				{
					upBuyTips=upBuyTips.replace("@needItemName",shopObj.itemName);
				}
				while(upBuyTips.indexOf("@pil")!=-1)
				{
					upBuyTips=upBuyTips.replace("@pil",1);
				}
				upBuyTips=upBuyTips.replace("@totalPrice",shopObj.price);
				upBuyTips=upBuyTips.replace("@moenytype",moenytype);
				upBuyTips=upBuyTips.replace("@itemNum",shopObj.perNum);
				
				ToolTip_Sprite.getInstance().setTip(this._collectItemContainer,new MyTipSprite(upBuyTips),ToolTip_Sprite.DIRECTION_LEFT);
			}
		}
			
	}
}